﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QS.Drawables;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using QS.Game.Logic.Objects;
using QS.Game.Logic.Objects.common;

namespace QS.Logic.Objects
{
    static class QObjectFactory
    {
        public static Dictionary<Type, QAbstractRenderer> m_LoadedRenderer = new Dictionary<Type, QAbstractRenderer>();

        public static IDrawableObject GetInstance(Type type, GraphicsDevice device, ContentManager cm, params object[] args)
        {
            IDrawableObject newObject = Activator.CreateInstance(type, args) as IDrawableObject;
            if (newObject == null)
                throw new ArgumentException();
            newObject.Load(device, cm);

            object[] list = type.GetCustomAttributes(typeof(RendererAttribute), true);

            if (list != null && list.Length > 0)
            {
                RendererAttribute att = type.GetCustomAttributes(typeof(RendererAttribute), true)[0] as RendererAttribute;
                QAbstractRenderer renderer = null;
                if (!m_LoadedRenderer.ContainsKey(att.RendererType))
                {
                    renderer = (QAbstractRenderer)Activator.CreateInstance(att.RendererType);

                    m_LoadedRenderer.Add(att.RendererType, renderer);
                    renderer.LoadContent(device, cm);
                }
                else
                    renderer = m_LoadedRenderer[att.RendererType];

                ((IDrawableObject)newObject).Renderer = renderer;
            }

            return newObject;
        }
    }
}
